package me.shiqiang.im.exception;

import com.fasterxml.jackson.core.JsonParseException;
import com.tst.common.MessageReader;
import com.tst.common.excel.ExcelException;
import com.tst.common.exception.ServiceException;
import com.tst.common.returnobj.BaseReturnObj;
import com.tst.common.returnobj.ParamErrorReturnObj;
import com.tst.common.returnobj.ReturnObjCode;
import org.hibernate.StaleObjectStateException;
import org.hibernate.exception.JDBCConnectionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * @author cuishiqiang
 * @date 2018/11/8 6:46 PM
 */

@ControllerAdvice
public class DefaultExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(com.tst.plugins.conf.DefaultExceptionHandler.class);

    public DefaultExceptionHandler() {
    }

    @ExceptionHandler({BindException.class})
    @ResponseBody
    public BaseReturnObj bindExceptionHandler(BindException e) {
        logger.error("参数验证异常", e);
        return new ParamErrorReturnObj(e.getBindingResult(), MessageReader.getMessage("msg.common.paramValidateFail"));
    }

    @ExceptionHandler({MethodArgumentNotValidException.class})
    @ResponseBody
    public BaseReturnObj methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        logger.error("参数验证异常", e);
        return new ParamErrorReturnObj(e.getBindingResult(), MessageReader.getMessage("msg.common.paramValidateFail"));
    }

    @ExceptionHandler({ExcelException.class})
    @ResponseBody
    public BaseReturnObj excelExceptionHandler(NativeWebRequest request, ExcelException e) {
        logger.error("Excel异常", e);
        return new BaseReturnObj(ReturnObjCode.BUSINESS_EXCEPTION, MessageReader.getMessage(e.getErrorCode(), e.getParams()));
    }

    @ExceptionHandler({ServiceException.class})
    @ResponseBody
    public BaseReturnObj serviceExceptionHandler(NativeWebRequest request, ServiceException e) {
        logger.error("业务异常", e);
        return new BaseReturnObj(ReturnObjCode.BUSINESS_EXCEPTION, MessageReader.getMessage(e.getErrorCode(), e.getParams()));
    }

    @ExceptionHandler({JDBCConnectionException.class})
    @ResponseBody
    public BaseReturnObj jdbcConnectionException(NativeWebRequest request, JDBCConnectionException e) {
        logger.error("jdbc异常", e);
        return new BaseReturnObj(ReturnObjCode.UNKOWN_ERROR, MessageReader.getMessage("msg.common.MySQLIsConnFail"));
    }

    @ExceptionHandler({JedisConnectionException.class})
    @ResponseBody
    public BaseReturnObj jedisConnectionException(NativeWebRequest request, JedisConnectionException e) {
        logger.error("Redis异常", e);
        return new BaseReturnObj(ReturnObjCode.UNKOWN_ERROR, MessageReader.getMessage("msg.common.RedisIsConnFail"));
    }

    @ExceptionHandler({StaleObjectStateException.class})
    @ResponseBody
    public BaseReturnObj staleObjectStateExceptionHandler(NativeWebRequest request, StaleObjectStateException e) {
        logger.error("乐观锁异常", e);
        return new BaseReturnObj(ReturnObjCode.UNKOWN_ERROR, MessageReader.getMessage("msg.common.versionException"));
    }

    @ExceptionHandler({JsonParseException.class})
    @ResponseBody
    public BaseReturnObj exceptionHandler(NativeWebRequest request, JsonParseException e) {
        logger.error("JSON转换错误", e);
        return new BaseReturnObj(ReturnObjCode.UNKOWN_ERROR, MessageReader.getMessage("msg.common.jsonParseException"));
    }

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public BaseReturnObj exceptionHandler(NativeWebRequest request, Exception e) {
        logger.error("未知错误", e);
        return new BaseReturnObj(ReturnObjCode.UNKOWN_ERROR, MessageReader.getMessage("msg.common.systemException"));
    }
}